package com.study.reactor.forkjoin;

import java.util.concurrent.RecursiveTask;

public class SumTask extends RecursiveTask<Integer> {

    private static Integer POINT = null;
    //要累加的源数组
    private int[] src;
    //开始角标
    private int startIndex;
    //结束角标
    private int endIndex;

    public SumTask(int[] src, int startIndex, int endIndex,Integer POINT) {
        this.src = src;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.POINT = POINT;
    }

    //实现具体的累加逻辑和任务分割逻辑
    @Override
    protected Integer compute() {
        //不满足阈值的时候，这里面的逻辑也是当满足阈值的时候，递归执行的逻辑
        if (endIndex - startIndex < POINT) {
            int count = 0;
            for (int i = startIndex; i <= endIndex; i++) {
                count += src[i];
//                    SleepTools.ms(1);
            }

            System.out.println("线程名称："+Thread.currentThread().getName());
            return count;

            //满足阈值的时候，需要分割任务，然后交给forkjoinpool去执行任务
        } else {

            //当需要分割的时候，采用折中法进行分割
            //startIndex.......mid.......endIndex
            int mid = (startIndex + endIndex) / 2;

            //左任务
            SumTask leftTask = new SumTask(src, startIndex, mid,POINT);
            //右任务
            SumTask rigthTask = new SumTask(src, mid + 1, endIndex,POINT);

            System.out.println("线程名称2："+Thread.currentThread().getName());
            //交给forkjoinpool去执行任务
            invokeAll(leftTask, rigthTask);

            //将执行结果返回
            return leftTask.join() + rigthTask.join();
        }
    }
}
